﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	public static class UnityRenderLib 
	{
		#region stuff for exporting to lua

		public static string LIB_NAME = "UnityRenderLib";

		public static string ParentLibName = "UnityComponentLib";

		#endregion

		#region variables

		public static int mf_IsCastShadows (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushBoolean (renderer.castShadows);
			return 1;
		}

		public static int mf_SetCastShadows (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			renderer.castShadows = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_IsEnabled (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushBoolean (renderer.enabled);
			return 1;
		}

		public static int mf_SetEnabled (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			renderer.enabled = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_IsPartOfStaticBatch (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushBoolean (renderer.isPartOfStaticBatch);
			return 1;
		}

		public static int mf_IsVisible (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushBoolean (renderer.isVisible);
			return 1;
		}

		public static int mf_GetLightmapIndex (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushInteger (renderer.lightmapIndex);
			return 1;
		}

		public static int mf_SetLightmapIndex (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			renderer.lightmapIndex = luaState.ToInteger (2);
			return 0;
		}

		public static int mf_GetLightmapTilingOffset (ILuaState luaState)
		{
			// TODO xiewneqi
			return 1;
		}

		public static int mf_SetLightmapTilingOffset (ILuaState luaState)
		{
			// TODO xiewneqi
			return 0;
		}

		public static int mf_GetMaterial (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			LuaTools.L_PushUserData (luaState, renderer.material, UnityMaterialLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetMaterial (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			Material mat = LuaTools.L_CheckUData (luaState, 2, UnityMaterialLib.LIB_NAME) as Material;
			renderer.material = mat;
			return 0;
		}

		public static int mf_GetMaterials (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			Material[] materials = renderer.materials;
			if (materials == null || materials.Length == 0)
			{
				luaState.PushNil ();
			}
			else
			{
				luaState.CreateTable (materials.Length, 0);
				for (int i = 0, len = materials.Length; i < len; ++ i)
				{
					LuaTools.L_PushUserData (luaState, materials[i], UnityMaterialLib.LIB_NAME);
					luaState.RawSetI (-2, i + 1);
				}
			}
			return 1;
		}

		public static int mf_SetMaterials (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			int arrayLength = luaState.L_Len (2);
			Material[] mats = new Material[arrayLength];
			for (int i = 0; i < arrayLength; ++ i)
			{
				luaState.RawGetI (2, i + 1);
				mats[i] = LuaTools.L_CheckUData (luaState, 3, UnityMaterialLib.LIB_NAME) as Material;
				luaState.Pop (1);
			}
			renderer.materials = mats;
			return 0;
		}

		public static int mf_IsReceiveShadows (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			luaState.PushBoolean (renderer.receiveShadows);
			return 1;
		}

		public static int mf_SetReceiveShadows (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			renderer.receiveShadows = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetSharedMaterial (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			LuaTools.L_PushUserData (luaState, renderer.sharedMaterial, UnityMaterialLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetSharedMaterial (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			Material mat = LuaTools.L_CheckUData (luaState, 2, UnityMaterialLib.LIB_NAME) as Material;
			renderer.sharedMaterial = mat;
			return 0;
		}

		public static int mf_GetSharedMaterials (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			Material[] materials = renderer.sharedMaterials;
			if (materials == null || materials.Length == 0)
			{
				luaState.PushNil ();
			}
			else
			{
				luaState.CreateTable (materials.Length, 0);
				for (int i = 0, len = materials.Length; i < len; ++ i)
				{
					LuaTools.L_PushUserData (luaState, materials[i], UnityMaterialLib.LIB_NAME);
					luaState.RawSetI (-2, i + 1);
				}
			}
			return 1;
		}

		public static int mf_SetSharedMaterials (ILuaState luaState)
		{
			Renderer renderer = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Renderer;
			int arrayLength = luaState.L_Len (2);
			Material[] mats = new Material[arrayLength];
			for (int i = 0; i < arrayLength; ++ i)
			{
				luaState.RawGetI (2, i + 1);
				mats[i] = LuaTools.L_CheckUData (luaState, 3, UnityMaterialLib.LIB_NAME) as Material;
				luaState.Pop (1);
			}
			renderer.sharedMaterials = mats;
			return 0;
		}


		#endregion
	}
}
